<template>
  <!-- 添加部门对话框 -->
  <el-dialog
    :title="title"
    :visible="showDialog"
    width="40%"
    @close="handleCloseDialog"
  >
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" :disabled="form.id ? true : false" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeeSimple } from '@/api/employees'
import { reqAddDepartments, reqGetDepartDetail, reqEditDepart } from '@/api/department'
export default {
  props: {
    // 接收父组件控制对话框布尔值
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    depList: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      // console.log(this.depList)
      if (this.form.id) {
        if (this.form.name === value) {
          callback()
          return
        }
      }
      let arr = []
      if (this.form.id) {
        arr = this.depList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.depList.filter(item => item.pid === this.nodeData.id)
      }
      // console.log(value)
      // console.log(this.nodeData.children)
      // if (!this.nodeData.children) {
      //   this.nodeData.children = []
      // }
      // if (this.nodeData.children.length) {
      //   const bo = this.nodeData.children.some(item => item.name === value)
      //   console.log(bo)
      //   bo ? callback(new Error(`已存在${value}`)) : callback()
      // } else {
      //   callback()
      // }
      // console.log(arr)
      // 3. 检测是否有重复
      const boo = arr.some(item => item.name === value)
      // console.log(boo)
      boo ? callback(new Error(`已经存在${value}部门`)) : callback()
    }
    // 编码自定义校验规则
    const checkCodeRepeat = (rule, value, cb) => {
      if (this.form.id) {
        if (this.form.code === value) {
          cb()
          return
        }
      }
      // 遍历所有原始数据, 拿到对应的每一条数据的code  判断和value是否重复
      const boo = this.depList.some(item => item.code === value)
      console.log(boo)
      boo ? cb(new Error(`已经存在${value}部门编码`)) : cb()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      peoples: []
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '添加部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    handleCloseDialog() {
      console.log(111)
      // 子传父
      this.$emit('update:showDialog', false)
      // this.$emit('closeDialog')
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
      this.$refs.deptForm.resetFields()
    },
    // 获取下拉列表数据方法
    async getEmployeeSimple() {
      const res = await reqGetEmployeeSimple()
      this.peoples = res.data
      console.log(this.peoples)
    },
    // 添加/编辑部门
    clickSubmit() {
      this.$refs.deptForm.validate(async item => {
        if (item) {
          if (this.form.id) {
          // 编辑部门
            const { message } = await reqEditDepart(this.form)
            this.$message.success(message)
          } else {
          // 添加部门
            // console.log('发请求')
            const { message } = await reqAddDepartments({
              ...this.form, pid: this.nodeData.id
            })
            this.$message.success(message)
          }
          // 通知到父组件, 重新获取数据
          this.$emit('add-depts')
          // 关闭弹层
          // this.$emit('closeDialog', false)
          this.$emit('update:showDialog', false)
        }
      })
    },
    // 获取部门详情
    async getDepartments(id) {
      const res = await reqGetDepartDetail(id)
      console.log(res)
      this.form = res.data
    }
  }
}
</script>

<style>

</style>
